﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeGeneratorHelper.Entities;
using CodeGeneratorHelper;

namespace CodeGeneratorUtility
{
    public class CodeBuilder
    {
        
        public static string GetEntityCode(List<ColumnInfo> columns,string tableName,bool IsEcn = false)
        {
            StringBuilder sbEntity = new StringBuilder();

            sbEntity.Append(" public class ").Append(tableName.Replace("tbl_","")).Append(Environment.NewLine).Append("{").Append(Environment.NewLine);

            foreach (ColumnInfo col in columns)
            {
                if (IsEcn)
                {
                    sbEntity.Append("public ").Append(UIHelper.GetECNDotNetType(col.Type)).Append(" ").Append(col.Name).Append(" { get; set; }").Append(Environment.NewLine);
                }
                else
                {
                    sbEntity.Append("public ").Append(UIHelper.GetDotNetType(col.Type)).Append(" ").Append(col.Name).Append(" { get; set; }").Append(Environment.NewLine);
                }

            }
            sbEntity.Append("}").Append(Environment.NewLine);
            return sbEntity.ToString();
        }
        public static string GetSPParameters(List<ColumnInfo> columns, string tableName)
        {
            StringBuilder sbParams = new StringBuilder();
            foreach (ColumnInfo col in columns)
            {
                sbParams.Append(col.GetDBParamStr()).Append(Environment.NewLine);
            }
            return sbParams.ToString();
        }
        public static string GetECNParameters(List<ColumnInfo> columns, string tableName)
        {
            StringBuilder sbParams = new StringBuilder();
            foreach (ColumnInfo col in columns)
            {
                sbParams.Append("cmd.Parameters.Add(new SqlParameter(\"@prm"+col.Name+"\","+col.Name+"));").Append(Environment.NewLine);
            }
            return sbParams.ToString();
        }
        public static string GetInsertScript(List<ColumnInfo> columns, string tableName)
        {
            StringBuilder sbInsert = new StringBuilder();
            sbInsert.Append(" INSERT INTO ").Append(tableName).Append(" (");

            int i = 0;
            foreach (ColumnInfo col in columns)
            {
                sbInsert.Append(col.Name).Append(",");
            }
            string sStr = GetLastCharectarRemovedString(sbInsert.ToString(),",");
            sbInsert.Clear();
            sbInsert.Append(sStr);
            sbInsert.Append(") VALUES(");
            StringBuilder sbParam = new StringBuilder();
            int newLine = 0;
            foreach (ColumnInfo col in columns)
            {
                sbParam.Append(UserSettings.ParamStr).Append(col.Name).Append(",");
                newLine++;
                if (newLine % 5 == 0)
                {
                    sbParam.Append(Environment.NewLine);
                }
            }
            sStr = GetLastCharectarRemovedString(sbParam.ToString(), ",");
            sbInsert.Append(sStr).Append(")");
            return sbInsert.ToString();

        }
        public static string GetUpdateScript(List<ColumnInfo> columns, string tableName)
        {
            StringBuilder sbUpdate = new StringBuilder();
            sbUpdate.Append(" UPDATE TABLE ").Append(tableName).Append(" SET ");

            StringBuilder sbParam = new StringBuilder();
            int newLine = 0;
            foreach (ColumnInfo col in columns)
            {
                sbParam.Append(col.Name).Append(" = ").Append(UserSettings.ParamStr).Append(col.Name).Append(",").Append(Environment.NewLine); ;
                newLine++;
                if (newLine % 3 == 0)
                {
                    sbParam.Append(Environment.NewLine);
                }
            }
            string sStr = GetLastCharectarRemovedString(sbParam.ToString(), ",");
            sbUpdate.Append(sStr);
            return sbUpdate.ToString();

        }
        public static string GetLastCharectarRemovedString(string stringWithChar,string charToRemove)
        {
            return stringWithChar.Substring(0,stringWithChar.Length-1);

        }
        public static string GetLoadEntity(List<ColumnInfo> columns, string tableName)
        {
            StringBuilder sbLoad = new StringBuilder();
            string EntityName = tableName.Replace("tbl_", "");
            sbLoad.Append(EntityName).Append(" o").Append(EntityName).Append(" = new ").Append(EntityName).Append("();").Append(Environment.NewLine);

            StringBuilder sbParam = new StringBuilder();
            foreach (ColumnInfo col in columns)
            {
                sbLoad.Append("o").Append(EntityName).Append(".").Append(col.Name).Append(" = ").Append("objReader[\"").Append(col.Name).Append("\"] != DBNull.Value ? ");
                sbLoad.Append(col.GetReaderConversionString()).Append(";").Append(Environment.NewLine);
            }
            
            return sbLoad.ToString();
            
        }
        public static string GetLoadECNEntity(List<ColumnInfo> columns, string tableName)
        {
            StringBuilder sbLoad = new StringBuilder();
            string EntityName = tableName.Replace("tbl_", "");
            sbLoad.Append(EntityName).Append(" o").Append(EntityName).Append(" = new ").Append(EntityName).Append("();").Append(Environment.NewLine);

            StringBuilder sbParam = new StringBuilder();
            foreach (ColumnInfo col in columns)
            {
                sbLoad.Append("o").Append(EntityName).Append(".").Append(col.Name).Append(" = ");
                sbLoad.Append(col.GetECNReaderConversionString()).Append(";").Append(Environment.NewLine);
            }

            return sbLoad.ToString();

        }
        
        public static string GetInputParameter(List<ColumnInfo> columns, string tableName)
        {
            StringBuilder sbLoad = new StringBuilder();
            string EntityName = tableName.Replace("tbl_", "");
            
            StringBuilder sbParam = new StringBuilder();
            foreach (ColumnInfo col in columns)
            {
                string sDBType = col.Type.ToString();
                if (col.Type == DBColumnType.Numeric || col.Type == DBColumnType.Decimal)
                {
                    sDBType = "Decimal";
                }
                if (col.Type == DBColumnType.Boolean)
                {
                    sDBType = "Bit";
                }
                
                string ParamName = UserSettings.ParamStr + col.Name;
                sbLoad.Append("SqlParameter ").Append(ParamName).Append(" = command.Parameters.Add(\"").Append(ParamName).Append("\", SqlDbType.").Append(sDBType).Append(");").Append(Environment.NewLine);
                sbLoad.Append(ParamName).Append(".Direction = ParameterDirection.Input;").Append(Environment.NewLine);
                sbLoad.Append(ParamName).Append(".Value = obj.").Append(col.Name).Append(";").Append(Environment.NewLine).Append(Environment.NewLine);
            }
            
            return sbLoad.ToString();
            
        }
        public static string GetMigrationXML(List<ColumnInfo> columns, string tableName)
        {
            StringBuilder sbXML = new StringBuilder();
            string EntityName = tableName.Replace("tbl_", "");

            foreach (ColumnInfo col in columns)
            {
                sbXML.Append("<column mediname=\"[").Append(col.Name).Append("]\" opname=\"").Append(col.Name).Append(" optype=\"").Append(UIHelper.GetDotNetType(col.Type)).Append(" isupdateable=\"true\" />").Append(Environment.NewLine);

            }

            return sbXML.ToString();

        }
        
    }
    
}